package cate.game.admin.menu.role;

import cate.common.table.d.GDOperation;
import cate.common.table.role.vip.RoleVipRow;
import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.event.master.VipChangeEvent;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.mail.Mail;
import cp.solution.util.time.DateKit;
import org.apache.commons.lang3.StringUtils;

public class RoleMenu extends MenuSetup {

    public RoleMenu(GameBody gameBody) {
        super("主角");
        reg(    gameBody,
                "改变主角等级",
                1,
                "参数1：等级(升级之后会额外送你1点经验使得推送经验等级改变消息)",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "参数不能少于1个!");
                        return;
                    }
                    int level = Integer.valueOf(params.get(0));
                    if (level < 0) {
                        gameBody.notice.message(master, "等级不能小于0");
                        return;
                    }
                    master.getBase().exp = 0;
//                    if (level > 200) {
//                        gameBody.notice.message(
//                                master, "等级不能高于200");
//                        return;
//                    }
                    int levelAdd = level - master.getBase().level;
                    if (levelAdd < 0) {
                        master.getBase().levelUp(levelAdd);
                        gameBody.notice.message(
                                master, "降级过程无法完全逆转游戏进程!");
                    } else if (levelAdd == 0) {
                        gameBody.notice.message(
                                master, String.format("不是已经%d级了吗？还想怎样？", level));
                    } else {
                        master.getBase().levelUp(levelAdd);
                        gameBody.notice.message(
                                master, "ヾ(｡｀Д´｡)升了那么多级");
                    }
                    master.getBase().addExp(1);
                });

        reg(
                gameBody,
                "主角升一级",
                0,
                "直接升级，不需要参数(升级之后会额外送你1点经验使得推送经验等级改变消息)",
                (master, params) -> {
                    master.getBase().levelUp(1);
                    gameBody.notice.message(master, "你已经升了一级了");
                    master.getBase().addExp(1);
                });

        reg(
                gameBody,
                "增加主角经验",
                1,
                "增加经验而已，加得够多才能升级，参数1：经验增加值",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "参数不能少于1个");
                        return;
                    }
                    long add = Long.valueOf(params.get(0));
                    master.getBase().addExp(add);
                    gameBody.notice.message(
                            master, String.format("已经给你加%d经验的了", add));
                });

        reg(
                gameBody,
                "改变主角vip等级",
                1,
                "参数1：vip等级",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "参数不能少于1个");
                        return;
                    }
                    int vip = Integer.valueOf(params.get(0));
                    // 升级邮件
                    RoleVipRow row = gameBody.table.vip.base.getByLevel(vip);
                    if(row == null){
                        gameBody.notice.message(master, "vip等级有误");
                        return;
                    }
                    int oldVip = master.getBase().vip;
                    master.getBase().vip = vip;
                    publishEvent(gameBody, new VipChangeEvent(master, oldVip, vip));
                    // 升级邮件
                    if (row.mailId > 0) {
                        Mail mail = new MailBuilder()
                                .setOperation(GDOperation.VIP_LEVEL_REWARD)
                                .setOperationSub(vip)
                                .setContentId(row.mailId)
                                .setReward(StringUtils.isNotBlank(row.levelRewardStr) ? new MixRes(row.levelRewardStr) : null)
                                .build();
                        master.getMail().add(mail);
                    }
                    master.getBase().noticeVipUpdate();
                    gameBody.notice.message(master,"vip升级成功");
                });

        reg(
                gameBody,
                "增加主角vip经验",
                1,
                "参数1：vip经验",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "参数不能少于1个");
                        return;
                    }
                    int vip = Integer.valueOf(params.get(0));
                    master.getBase().addVipPoint(vip);
                });

        reg(
                gameBody,
                "圣器来一发",
                1,
                "食神之路暂时不开发，用gm工具造圣器吧, 参数1：圣器类型",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "你没传类型噢");
                        return;
                    }
                    master.getRes().leader.hallows.unlock(Integer.parseInt(params.get(0)));
                }
        );

        reg(
                gameBody,
                "设置主角阶数",
                1,
                " 参数1：阶数",
                (master, params) -> {
                    if (params.size() < 1) {
                        gameBody.notice.message(master, "阶数");
                        return;
                    }
                    master.getBase().grade = Integer.parseInt(params.get(0));
                    gameBody.notice.message(master,"成功");
                }
        );

        reg(
                gameBody,
                "重置所有圣器的等级",
                0,
                "",
                (master, params) -> {
                    master.getRes().leader.hallows.gmResetAll();
                    gameBody.notice.message(master,"重置成功");
                }
        );

        reg(
                gameBody,
                "修改创角时间",
                1,
                "【参数1】当前是创角第几天",
                (master, params) -> {
                    int openDay = Integer.parseInt(params.get(0)) - 1;
                    if (openDay < 0) {
                        openDay = 0;
                    }
                    //  master.getStatus().createTime = TimeTool.calTimeStamp(gameBody.getOpenTime(), openDay);
                    master.getStatus().createTime = gameBody.time.now() - openDay * DateKit.DAY_MS;
                    gameBody.notice.message(master, "修改成功，请重新登录");
                    //gameBody.role.saveAndDrop(master);
                }
        );
    }
}
